//
//  BFShOrderDetailViewController.m
//  BeautyFood
//
//  Created by apple on 2020/5/5.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "BFShOrderDetailViewController.h"
#import "BFShCommitOrderItemView.h"
@interface BFShOrderDetailViewController ()
@property(nonatomic, strong) BFShOrderModel                     *model;/**<  */
@property(nonatomic, strong) UIView                     *bootomView;/**<  */
@property(nonatomic, strong) NSMutableArray                     *itemsarray;/**<  */

@end

@implementation BFShOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self refresh_detaiil_model];
    // Do any additional setup after loading the view.
}
- (void)refresh_detaiil_model{
    [BFShOrderDataController stoneobs_fun_send_detail_order:self.o_id handle:^(BFShOrderModel * _Nonnull model) {
        self.model = model;
        [self stoneobs_ui_configSubView];
    }];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    [self.bootomView removeFromSuperview];
    __weak typeof(self) weakSelf =  self;
    if (self.model) {
        [self configBootomView];
        
        UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
        header.backgroundColor = UIColor.whiteColor;
        CGFloat top = 15;
        __weak typeof(self) weakSelf =  self;
        
        
        ({
            
            
            
            STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(15, top, STONEOBS_UI_UISCREEN_WIDTH - 30, 30)
                                                                      title:[NSString stringWithFormat:@"%@  %@",self.model.user_name,self.model.phone]
                                                                 titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                  titleFont:15
                                                               cornerRadius:0
                                                            backgroundColor:nil
                                                            backgroundImage:nil
                                                                      image:nil];
            [header addSubview:stoneobs_ui_button];
            [stoneobs_ui_button setAttributedTitle:[stoneobs_ui_button.currentTitle stoneobs_fun_st_convertAttributeStringWithKeyWord:@"" attributes:@{NSForegroundColorAttributeName:STONEOBS_UI_THIRD_TEXT_COLOR,NSFontAttributeName:[UIFont systemFontOfSize:13]}] forState:UIControlStateNormal];
            
            NSString * address = [NSString stringWithFormat:@"%@",self.model.detail];
            STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, stoneobs_ui_button.bottom + 10, stoneobs_ui_button.width - 40, 30)
                                                                    text:address
                                                               textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                    font:15
                                                             isSizetoFit:NO
                                                           textAlignment:NSTextAlignmentLeft];
            [header addSubview:stoneobs_ui_label];;
            stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:17];
            [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_label];
            top = stoneobs_ui_label.bottom  +20;
            
            UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 7, 12)];
            stoneobs_ui_imageView.image = [UIImage imageNamed:@"圆角矩形580"];
            [header addSubview:stoneobs_ui_imageView];
            stoneobs_ui_imageView.centerY = stoneobs_ui_label.centerY;
            stoneobs_ui_imageView.right = UISCREEN_WIDTH - 15;
            
            
        });
        
        ({
            UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(6, top, UISCREEN_WIDTH-12, 4.5)];
            stoneobs_ui_imageView.image = [UIImage imageNamed:@"条纹"];
            [header addSubview:stoneobs_ui_imageView];
            top = stoneobs_ui_imageView.bottom;
        });
        
        ({
            UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH, 20)];
            stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
            [header addSubview:stoneobs_ui_line];
            top = stoneobs_ui_line.bottom;
            [stoneobs_ui_line stoneobs_fun_stoneobs_fun_st_showRoundLayer];
        });
        
        self.itemsarray = NSMutableArray.new;
        
        for (BFShOrderModelGoodsModel * model in self.model.details) {
            BFShCommitOrderItemView * view = [[BFShCommitOrderItemView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
            view.goodModel = model;
            view.top = top;
            top = view.bottom;
            [header addSubview:view];
            [self.itemsarray addObject:view];
            view.backgroundColor = UIColor.whiteColor;
            view.layer.cornerRadius = 10;
            view.clipsToBounds = YES;
            ({
                UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH, 20)];
                stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
                [header addSubview:stoneobs_ui_line];
                top = stoneobs_ui_line.bottom;
            });
        }
        
        
        ({
            
            STButton * button = [self viewFormLeftTitle:@"订单信息" value:@""];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
            UILabel * lable = [button viewWithTag:10002];
            lable.text = @"订单信息";
            lable.textColor = UIColor.blackColor;
            lable.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:12];
        });
        
        
        ({
            
            STButton * button = [self viewFormLeftTitle:@"订单编号" value:self.model.order_no];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
            
            STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 40, button.height)
                                                                      title:@"复制"
                                                                 titleColor:FlatBlue
                                                                  titleFont:12
                                                               cornerRadius:0
                                                            backgroundColor:nil
                                                            backgroundImage:nil
                                                                      image:nil];
            [button addSubview:stoneobs_ui_button];
            [stoneobs_ui_button setClicAction:^(UIButton *sender) {
                [UIPasteboard.generalPasteboard setString:weakSelf.model.order_no];
                [SVProgressHUD showSuccessWithStatus:@"复制成功"];
            }];
            stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            stoneobs_ui_button.right = UISCREEN_WIDTH - 10;
        });
        
        
        ({
            NSString * value = [self.model.pay_info stringByReplacingOccurrencesOfString:@";" withString:@"\n"];
            value = self.model.pay_info;
            STButton * button = [self viewFormLeftTitle:@"支付方式" value:value.length?value:@""];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
        });
        
        ({
            
            STButton * button = [self viewFormLeftTitle:@"下单时间" value:self.model.created_at];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
        });
        
        if (self.model.pay_info.length > 1) {
            STButton * button = [self viewFormLeftTitle:@"支付时间" value:self.model.pay_at];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
        }
        if (self.model.send_at.length > 1) {
            STButton * button = [self viewFormLeftTitle:@"发货时间" value:self.model.send_at];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
        }
        if (self.model.finish_at.length > 1) {
 
        }
        if (self.model.integral.floatValue > 0) {
            NSInteger money = self.model.integral.floatValue / 1;
            NSString * value = [NSString stringWithFormat:@"使用%@积分抵扣了%@元",self.model.integral,@(money)];
            STButton * button = [self viewFormLeftTitle:@"积分" value:value];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
        }
        
        ({
            STButton * button = [self viewFormLeftTitle:@"邮费" value:self.model.postage];
            button.top = top;
            top = button.bottom;
            [header addSubview:button];
        });
        ({
            UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH, 30)];
            stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
            [header addSubview:stoneobs_ui_line];
            top = stoneobs_ui_line.bottom;
        });
        header.height = top + 0;
        self.tableView.tableHeaderView = header;
        
    }else{
        
        STTools_NoresultView *st_noreslutView = [[STTools_NoresultView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 400) title:@"暂无数据" buttonTitle:@"重试" buttonHandle:^(NSString *titleString) {
            
            [weakSelf refresh_detaiil_model];
        }];
        self.tableView.tableHeaderView = st_noreslutView;
    }
    
    
    
}
- (void)configBootomView{
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_tabBarTop, STONEOBS_UI_UISCREEN_WIDTH, 49 + 50)];
    [self.view addSubview:bootomView];
    bootomView.backgroundColor = UIColor.whiteColor;
    self.bootomView = bootomView;
    [bootomView stoneobs_fun_st_showTopShadow];
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(16, 0, 200, 49)
                                                            text:[NSString stringWithFormat:@"￥%0.2f",self.model.total_price.floatValue]
                                                       textColor:WH_UICOLOR_FROM_RGBA(0xFF2F3D)
                                                            font:15
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [bootomView addSubview:stoneobs_ui_label];
    __weak typeof(self) weakSelf =  self;
    
    NSArray * array = [BFShOrderFooterView arrayFormModel:self.model];
    CGFloat right = STONEOBS_UI_UISCREEN_WIDTH - 10;
    for (NSString * name in array) {
        STButton * button = [BFShOrderFooterView buttonFormTitle:name];
        button.centerY = 24.5;
        [bootomView addSubview:button];
        button.right = right;
        right = button.left - 10;
        [button setClicAction:^(UIButton *sender) {
            
            [BFShOrderHomeViewController onSelctedMenuTitle:name model:weakSelf.model];
        }];
        
    }
    
}
- (STButton*)viewFormLeftTitle:(NSString*)leftTitle value:(NSString*)value{
    STButton * view  = [[STButton alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 35)
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:nil
                                       backgroundImage:nil
                                                 image:nil];
    
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(17, 0, 120, view.height)
                                                            text:[NSString stringWithFormat:@"%@:",leftTitle]
                                                       textColor:STMC_UICOLOR_FROM_RGBA(0x343434)
                                                            font:14
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_label];
    [NSString stoneobs_fun_lableAutoAdjustWitdhWithLabel:stoneobs_ui_label];
    stoneobs_ui_label.tag = 10002;
    
    STLabel * stoneobs_ui_s_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_label.right, 0, STONEOBS_UI_UISCREEN_WIDTH -stoneobs_ui_label.right - 20 , view.height)
                                                              text:value
                                                         textColor:STMC_UICOLOR_FROM_RGBA(0x343434)
                                                              font:14
                                                       isSizetoFit:NO
                                                     textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_s_label];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_s_label];
    if (stoneobs_ui_s_label.height < view.height) {
        stoneobs_ui_s_label.height = view.height;
    }else{
        view.height = stoneobs_ui_s_label.height + 20;
    }
    stoneobs_ui_label.centerY = view.height/2;
    stoneobs_ui_s_label.centerY = view.height/2;
    stoneobs_ui_s_label.tag = 10001;
    UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(17, view.height - 0.5, STONEOBS_UI_UISCREEN_WIDTH, 5)];
    stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
    //    [view addSubview:stoneobs_ui_line];
    __weak typeof(self) weakSelf =  self;
    return view;
    
}

@end

